/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.repository;

import com.bifrost.entity.Customer;
import com.bifrost.entity.MerchantStore;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;

/**
 * Date: 2017/9/28
 * Time: 22:16
 *
 * @author bifrost
 * @version 1.0.0
 */
public interface CustomerRepository extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
    Customer findFirstByCustomerPhone(String phone);

    Customer findByOpenId(String openId);

    /**
     * 增加积分
     *
     * @param customerId
     * @param value
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE CUSTOMER SET BONUS_POINTS = BONUS_POINTS + ?2 WHERE CUSTOMER_ID =?1", nativeQuery = true)
    int incrementBonusPoints(Long customerId, BigInteger value);

    /**
     * 扣减积分
     *
     * @param customerId
     * @param value
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE CUSTOMER SET BONUS_POINTS = BONUS_POINTS - ?2 WHERE CUSTOMER_ID =?1 AND BONUS_POINTS >=?2", nativeQuery = true)
    int decrementBonusPoints(Long customerId, BigInteger value);

    /**
     * 查询门店会员卡所对应的会员
     *
     * @param store
     * @param cardNumber
     * @return
     */
    Customer findFirstByMerchantStoreAndLoyaltyCard(MerchantStore store, String cardNumber);

    @Query(value = "select * from customer where MERCHANT_ID=?1 and CUSTOMER_PHONE=?2", nativeQuery = true)
    Customer findCustomerByPhone(Long merchantId, String customerPhone);

    @Query(value = "select * from customer where MERCHANT_ID=?1 and LOYALTY_CARD=?2", nativeQuery = true)
    Customer findCustomerByCard(Long merchantId, String cardNumber);

    List<Customer> findAllByCustomerPhoneAndMerchantStore(String customerPhone, MerchantStore store);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "UPDATE CUSTOMER SET BONUS_POINTS = BONUS_POINTS + ?1 WHERE BONUS_POINTS =?2 AND CUSTOMER_ID =?3", nativeQuery = true)
    int saveWithOriginalBonusPoints(BigInteger newBonusPoint, BigInteger oldBonusPoint, Long customerId);

    @Modifying(clearAutomatically = true)
    @Query(value = "update customer set BONUS_POINTS=0 where MERCHANT_ID=?1", nativeQuery = true)
    int clearBonus(Long storeId);

    @Query(value = "select count(*) from customer where MERCHANT_ID=?1 and BONUS_POINTS!=0", nativeQuery = true)
    int checkBonusIsClear(Long storeId);

    Customer findFirstByLoyaltyCard(String cardNumber);

    @Query(value = "select * from customer where MERCHANT_ID=?1", nativeQuery = true)
    List<Customer> findCustomerByStoreId(Long storeId);

    @Query(value = "select count(CUSTOMER_ID) from customer where MERCHANT_ID=?1", nativeQuery = true)
    Long getTotalCustomers(Long storeId);

    @Query(value = "select sum(BONUS_POINTS) from customer where MERCHANT_ID=?1", nativeQuery = true)
    BigInteger getTotalBonus(Long storeId);

    @Query(value = "select MERCHANT_ID,count(CUSTOMER_ID),sum(BONUS_POINTS) from customer WHERE LOYALTY_CARD is not null GROUP BY MERCHANT_ID ", nativeQuery = true)
    List<Object[]> queryTotal();

    @Transactional
    @Query(value = "UPDATE CUSTOMER SET BONUS_POINTS = ?2 WHERE CUSTOMER_ID =?1", nativeQuery = true)
    @Modifying
    int updateBonusPoints(Long customerId, BigInteger bigInteger);

    @Query(value = "SELECT CUSTOMER_ID FROM customer WHERE CUSTOMER_LASTNAME LIKE ?1 AND MERCHANT_ID=?2",nativeQuery = true)
    List<Long> findNameLikeAndStoreId(String name,Long storeId);

    @Query(value = "SELECT LOYALTY_CARD FROM customer WHERE CUSTOMER_LASTNAME LIKE ?1 AND MERCHANT_ID=?2",nativeQuery = true)
    List<String> findCardNumberByNameLikeAndStoreId(String name,Long storeId);

    @Query(value = "SELECT * FROM CUSTOMER WHERE MERCHANT_ID=?1 AND CUSTOMER_PHONE =?2 AND LOYALTY_CARD IS NOT NULL ",nativeQuery = true)
    List<Customer> findByStoreIdAndPhone(Long merchantId , String phone);

    Customer findFirstByCustomerPhoneAndLoyaltyCardIsNotNull(String mobile);
    List<Customer> findBySyncFalseAndLoyaltyCardNotNullAndMerchantStoreNotNull();

}